package com.showcai.controller.order;

import com.showcai.controller.base.WeChatBaseController;
import com.showcai.module.ExpressInfo;
import com.showcai.myRunTime.MyRunTimeExcption;
import com.showcai.result.ResultObj;
import com.showcai.result.ReturnCode;
import com.showcai.service.ExpressInfoService;
import com.showcai.tools.KdniaoSubscribeAPI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Baggio on 2018-9-25.
 */
@RequestMapping("/expressInfo")
@Controller
public class ExpressInfoController extends WeChatBaseController {
    @Autowired
    ExpressInfoService expressInfoService;


    @RequestMapping(value = "/expressInfo")
    public ModelAndView expressInfo() {
        ModelAndView mov = new ModelAndView();
        String views = "order/web/expressInfo";
        if (getIsMobile()) {
            views = "order/wap/expressInfo";
        }
        mov.setViewName(views);
        return mov;
    }


    @RequestMapping(value = "/getExpressInfoByOrderId")
    @ResponseBody
    public String getExpressInfoByOrderId(Long orderId) {
        ResultObj resultObj = new ResultObj();
        try {
            List<String> info = new ArrayList<>();

            ExpressInfo expressInfo = expressInfoService.selectExpressInfoByOrderId(orderId);

            String expCode = expressInfo.getExpressCode();
            String expNo = expressInfo.getExpressOrderId();
            info.add(KdniaoSubscribeAPI.getOrderTracesByJson(expCode, expNo));
            resultObj.setData(info);
        } catch (MyRunTimeExcption myRunTimeExcption) {
            myRunTimeExcption.printStackTrace();
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(myRunTimeExcption.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(e.getMessage());
        }
        return resultObj.toJson();
    }
}
